﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<script>

    function showName(cellValue, options, rowObject) {
        return "<a href=\"javascript:void(0);\" onclick=\"btn_details(" + rowObject.id + ")\">" + rowObject.name + "</a>";
    }

    function showState(cellValue, options, rowObject) {
        return "<a href=\"javascript:void(0);\" onclick=\"set_state(" + rowObject.id + ","+(rowObject.deleteMark ? "false":"true")+")\"><i class=\"fa fa-toggle-" + (rowObject.deleteMark ? "off":"on") + "\"></i></a>";
    }

    $(function () {
        gridList();
    })
    function gridList() {
        var $gridList = $("#gridList");
        $gridList.dataGrid({
            treeGrid: true,
            treeGridModel: "adjacency",
            ExpandColumn: "code",
            url: "/WorkFlow/WFCategory/GetTreeGridJson",
            height: $(window).height() - 96,
            colModel: [
                { label: "主键", name: "id", hidden: true, key: true },
                { label: '名称', name: 'name', width: 150, align: 'left', formatter: showName },
                { label: '编码', name: 'code', width: 100, align: 'left' },
                {
                    label: '父级', name: 'parentName', width: 100, align: 'left'
                },
                {
                    label: '创建时间', name: 'createTime', width: 80, align: 'left',
                    formatter: "date", formatoptions: { srcformat: 'Y-m-d', newformat: 'Y-m-d' }
                },
                {
                    label: "有效", name: "deleteMark", width: 60, align: "center",
                    formatter: showState
                },
                { label: '备注', name: 'remark', width: 300, align: 'left' }
            ],
            onSelectRow: null
        });
        $("#btn_search").click(function () {
            $gridList.jqGrid('setGridParam', {
                postData: { keyword: $("#txt_keyword").val() },
            }).trigger('reloadGrid');
        });
    }
    function btn_add() {
        $.modalOpen({
            id: "Form",
            title: "新增分类",
            url: "/WorkFlow/WFCategory/Form",
            width: "700px",
            height: "300px",
            callBack: function (iframeId, index) {
                top.frames[iframeId].submitForm(index);
            }
        });
    }

    function btn_edit() {
        if (!$("#gridList").getGridParam("reccount"))
            return;

        var row = $("#gridList").jqGridRowValue();
        if (!row) {
            $.modalAlert("请选择需要的编辑的项", "error");
            return;
        }

        $.modalOpen({
            id: "Form",
            title: "编辑分类",
            url: "/WorkFlow/WFCategory/Form?keyValue=" + row.id,
            width: "700px",
            height: "300px",
            callBack: function (iframeId, index) {
                top.frames[iframeId].submitForm(index);
            }
        });
    }

    function btn_delete() {
        if (!$("#gridList").getGridParam("reccount"))
            return;

        //单选
        var ids = $("#gridList").jqGridRowValue();
        if (!ids) {
            $.modalAlert("请选择需要删除的项", "error");
            return;
        }

        ids = [ids.id];

        $.modalConfirm("确定要删除所选项吗？", function (result,index) {
            if (result) {
                $.ajax({
                    url: "/WorkFlow/WFCategory/Remove",
                    type: "POST",
                    data: { ids: ids.join(","),__RequestVerificationToken: $('[name=__RequestVerificationToken]').val() },
                    success: function (rsp) {
                        if (rsp.message) {
                            $.modalAlert(rsp.message, rsp.state);
                        }
                        $("#gridList").trigger("reloadGrid");
                    }
                });
            }
            top.layer.close(index);
        });
    }
    function btn_details(id) {

        if (!id)
            return;

        $.modalOpen({
            id: "Details",
            title: "查看",
            url: "/WorkFlow/WFCategory/Details?keyValue=" + id,
            width: "700px",
            height: "300px",
            btn: null,
        });
    }

    function set_state(id, s) {

        if (!$("#gridList").getGridParam("reccount"))
            return;

        if (s === undefined) {
            return;
        }

        if (id === null) {
            id = $("#gridList").jqGridRowValue();
            if (!id) {
                $.modalAlert("请选择需要设置的项！", "error");
                return;
            }

            id = id.id;
        }

        $.ajax({
            url: "/WorkFlow/WFCategory/DeleteForm",
            type: "POST",
            data: { ids: id,state:s, __RequestVerificationToken: $('[name=__RequestVerificationToken]').val() },
            success: function (rsp) {
                if (rsp.message) {
                    $.modalAlert(rsp.message, rsp.state);
                }
                $("#gridList").trigger("reloadGrid");
            }
        });
    }
</script>

<div class="topPanel">
    <div class="search">
        <table>
            <tr>
                <td>
                    <div class="input-group">
                        <input id="txt_keyword" type="text" class="form-control" placeholder="请输入要查询关键字" style="width: 200px;">
                        <span class="input-group-btn">
                            <button id="btn_search" type="button" class="btn  btn-primary"><i class="fa fa-search"></i></button>
                        </span>
                    </div>
                </td>
            </tr>
        </table>
    </div>
    <div class="toolbar">
        <div class="btn-group">
            <a id="NF-add" class="btn btn-primary dropdown-text" onclick="btn_add()"><i class="fa fa-plus"></i>新建</a>
        </div>
        <div class="btn-group">
            <a id="NF-edit" class="btn btn-primary dropdown-text" onclick="btn_edit()"><i class="fa fa-plus"></i>编辑</a>
        </div>
        <div class="btn-group">
            <a id="NF-delete" class="btn btn-primary dropdown-text" onclick="btn_delete()"><i class="fa fa-plus"></i>删除</a>
        </div>
    </div>
</div>
<div class="gridPanel">
    <table id="gridList"></table>
</div>
